package com.wrb.appointment.service;

import com.wrb.appointment.exception.ServiceException;
import com.wrb.appointment.feign.CouponServiceFeign;
import com.wrb.appointment.feign.CustomerServiceFeign;
import com.wrb.appointment.feign.PointsServiceFeign;
import com.wrb.appointment.mapper.AppointmentCustomerRecordMapper;
import com.wrb.appointment.mapper.AppointmentMapper;
import com.wrb.appointment.model.Appointment;
import com.wrb.appointment.model.AppointmentCriteria;
import com.wrb.appointment.model.AppointmentCustomerRecord;
import com.wrb.appointment.model.Customer;
import com.wrb.appointment.response.ResponseCode;
import com.wrb.appointment.response.ServerResponse;
import com.wrb.appointment.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wrb
 * @date 2019/12/6 11:40
 */
@Service
public class AppointmentService {

	@Autowired
	private AppointmentMapper appointmentMapper;

	@Autowired
	private AppointmentCustomerRecordMapper appointmentCustomerRecordMapper;

	@Autowired
	private CustomerServiceFeign customerServiceFeign;

	@Autowired
	private PointsServiceFeign pointsServiceFeign;

	@Autowired
	private CouponServiceFeign couponServiceFeign;

	public List getList() {
		AppointmentCriteria criteria = new AppointmentCriteria();
		criteria.setOrderByClause(" create_time desc");
		return this.appointmentMapper.selectByExample(criteria);
	}

	public Appointment selectByKey(Long appointmentId) {
		return this.appointmentMapper.selectByPrimaryKey(appointmentId);
	}

	/**
	 * 会员预约
	 *
	 * @param appointmentId
	 * @param customerId
	 * @throws ServiceException
	 */
	public void confirmAppointment(Long appointmentId, Long customerId) throws ServiceException {
		// 获取会员信息
		ServerResponse<Customer> customerResponse = this.customerServiceFeign.get(customerId);
		Customer customer = ServerResponse.parseResponse(customerResponse);
		// 获取预约信息
		Appointment appointment = this.appointmentMapper.selectByPrimaryKey(appointmentId);
		if (appointment == null) {
			throw new ServiceException(ResponseCode.APPOINTMENT_NOT_FOUND, "预约找不到");
		}
		// TODO 分布式事务
		//扣除用户积分
		ServerResponse pointsResponse = this.pointsServiceFeign.pointsChange(customerId, appointment.getNeedPoints(), appointment.getName(), -1);
		ServerResponse.parseResponse(pointsResponse);
		//发放优惠券
		ServerResponse<String> couponResponse = this.couponServiceFeign.generateCustomerCoupon(customerId, appointment.getCouponTemplateId(), appointment.getName());
		String code = ServerResponse.parseResponse(couponResponse);
		//生成预约记录
		AppointmentCustomerRecord record = new AppointmentCustomerRecord();
		record.setRecordId(SnowflakeIdWorker.generateId());
		record.setAppointmentId(appointmentId);
		record.setCustomerId(customerId);
		record.setNickname(customer.getNickname());
		record.setName(appointment.getName());
		record.setCode(code);
		int result = this.appointmentCustomerRecordMapper.insertSelective(record);
		if (result == 0) {
			throw new ServiceException(ResponseCode.DB_ERROR, "增加预约记录失败");
		}
	}

}
